package esi.t2no1.dao.meterdata;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.hibernate.validator.constraints.Length;

import com.supplier.MeteringMessage;

import ee.ut.cs.esi.t2es2.services.Client;
import ee.ut.cs.esi.t2es2.services.MeterData;
import esi.t2no1.ws.Ws;

/**
 * 
 * @author Reimo Rebane, Taavi Pauskar
 *
 */
public class MeterDataDTO {
	private Long id;
	@NotNull
	@Length(max=254,min=1)
	private String areaId;
	@NotNull
	@Length(max=254,min=1)
	private String facilityId;
	@NotNull
	@Length(max=254,min=1)
	private String readValue;
	@NotNull
	@Valid
	private Date readTime;

    public MeterDataDTO() {
        // Default constructor
    }

    public MeteringMessage toES1MeterData() {
    	Format formatter = new SimpleDateFormat("yyyy-MM-dd");
        MeteringMessage o = new MeteringMessage();
        try {
        	o.setAreaId(Integer.parseInt(areaId));
        } catch (NumberFormatException e) {
        	o.setAreaId(1337); // FIXME
        }
        try {
        	o.setFacilityId(Integer.parseInt(facilityId));
        } catch (NumberFormatException e) {
        	o.setFacilityId(1337); // FIXME
        }
        try {
        	o.setMeterValue(Integer.parseInt(readValue));
        } catch (NumberFormatException e) {
        	o.setMeterValue(1337); // FIXME
        }
        o.setDate(formatter.format(readTime));
        o.setNOId(Ws.NETWORK_OWNER_ID);

        return o;
    }

	public MeterData toES2MeterData() throws Exception {
        MeterData o = new MeterData();

        DatatypeFactory df = DatatypeFactory.newInstance();
        GregorianCalendar c = new GregorianCalendar();
        c.setTime(new Date());
        XMLGregorianCalendar now = df.newXMLGregorianCalendar(c);

        o.setMessageId(String.valueOf(System.currentTimeMillis()));
        o.setMessageRecipient("T2ES2");
        o.setSendTime(now);
        o.setMessageSender(Ws.NETWORK_OWNER_ID);
        o.setRelatedMessageId("");
        o.setReadingValue(readValue);
        o.setEstimated(false);
        o.setReadingStartDate(now);
        o.setReadingEndDate(now);
        o.setClient(new Client()); // FIXME We currently don't have the client information

        return o;
    }

    public String getAreaId() {
        return areaId;
    }

    public void setAreaId(String areaId) {
        this.areaId = areaId;
    }

    public String getFacilityId() {
        return facilityId;
    }

    public void setFacilityId(String facilityId) {
        this.facilityId = facilityId;
    }

    public String getReadValue() {
        return readValue;
    }

    public void setReadValue(String readValue) {
        this.readValue = readValue;
    }

    public Date getReadTime() {
        return readTime;
    }

    public void setReadTime(Date readTime) {
        this.readTime = readTime;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}
